package com.toommi.refuse.controller.admin;

import com.toommi.refuse.annotation.PermissionController;
import com.toommi.refuse.annotation.PermissionReport;
import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.SysUser;
import com.toommi.refuse.exception.AdminException;
import com.toommi.refuse.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * ..
 * 后台用户操作
 *
 * @author yangg
 * @since 2019-07-29 11:33
 */
@RestController
@Slf4j
@RequestMapping("/admin")
@AllArgsConstructor
@PermissionController(groupCode = "admin_user", groupName = "后台帐号管理")
@Api(tags = "后端管理帐号")
public class UserController {

    private SysUserService sysUserService;


    /**
     * 后台用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 登录结果及登录凭证
     */
    @ApiOperation(value = "后台用户登录", notes = "后台用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
    }
    )
    @PostMapping(value = "/login", produces = "application/json;charset=UTF-8")
    public CommonResponse login(@RequestParam String username, @RequestParam String password) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            Subject subject = SecurityUtils.getSubject();

            Session session = subject.getSession();
            String sessionId = (String) session.getId();
            commonResponse.setData(sessionId);
        } catch (Exception e) {
            log.error("admin user login error:{}", e);
            commonResponse = CommonResponse.serverError();
        }

        return commonResponse;
    }

    /**
     * 功能描述: 后台用户登出
     *
     * @param: []
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/20 0020 下午 14:44
     */
    @GetMapping(value = "/logOut")
    public CommonResponse loginOut() {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            Subject subject = SecurityUtils.getSubject();
            if (subject.isAuthenticated()) {
                subject.logout();// session 会销毁，在SessionListener监听session销毁，清理权限缓存
            }
        } catch (Exception e) {
            log.error("admin user login out error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /*
     * 功能描述: 后台管理修改密码
     * @param: [pass, newpass]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/23 0023 上午 9:38
     */
    @PostMapping(value = "/modifyPass", produces = "application/json;charset=UTF-8")
    public CommonResponse modifyPass(String pass, String newpass) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            Map<String, Object> res = sysUserService.modifyPass(pass, newpass);
            commonResponse.setMessage((String) res.get("msg"));
            commonResponse.setCode((Integer) res.get("code"));
        } catch (Exception e) {
            log.error("update password error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 新增管理帐号
     *
     * @param sysUser 管理帐号
     * @return 操作结果
     */
    @ApiOperation(value = "新增管理帐号", notes = "新增管理帐号")
    @ApiImplicitParam(name = "sysUser", value = "用户实体", required = true, dataType = "SysUser")
    @RequiresPermissions("admin:user:add")
    @PermissionReport(value = "admin:user:add", group = "admin_user", name = "添加帐号")
    @PostMapping(value = "/user", produces = "application/json;charset=UTF-8")
    public CommonResponse addAdmin(@RequestBody SysUser sysUser) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            sysUserService.addSysUser(sysUser);
        } catch (AdminException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add admin user error:{}", e);
        }

        return CommonResponse.success();
    }

    /**
     * 删除指定管理员帐号信息
     *
     * @param id 被删除帐号Id
     * @return 删除结果
     */
    @ApiOperation(value = "删除指定管理员帐号信息", notes = "删除指定管理员帐号信息")
    @ApiImplicitParam(name = "id", value = "管理帐号id", required = true, dataType = "Long")
    @RequiresPermissions("admin:user:delete")
    @PermissionReport(value = "admin:user:delete", name = "删除帐号", group = "admin_user")
    @DeleteMapping(value = "/user/{id}", produces = "application/json;charset=UTF-8")
    public CommonResponse deleteAdmin(@PathVariable(name = "id") long id) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            sysUserService.deleteUser(id);
        } catch (Exception e) {
            log.error("delete admin user error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 获取管理帐号列表
     *
     * @param page    页码
     * @param limit   每页数据量
     * @param roleId  角色id
     * @param keyWord 查询关键字
     * @return 管理帐号列表
     */
    @ApiOperation(value = "获取管理帐号列表", notes = "获取管理帐号列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数据量", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "roleId", value = "角色id", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "keyWord", value = "关键字", required = false, dataType = "string")
    })
    @GetMapping(value = "/users", produces = "application/json;charset=UTF-8")
    public Object findAdmins(@RequestParam int page, @RequestParam int limit,
                             long roleId, String keyWord) {
        //CommonResponse commonResponse = CommonResponse.success();
        try {
            return sysUserService.findSysUsers(page, limit, roleId, keyWord);
        } catch (Exception e) {
            log.error("find admin users error:{}", e);

        }
        return null;
    }

    /**
     * 获取微信帐号列表
     *
     * @param page  页码
     * @param limit 每页数据量
     * @return 管理帐号列表
     */
    @ApiOperation(value = "获取微信帐号列表", notes = "获取微信帐号列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数据量", required = true, dataType = "Long"),

    })
    @GetMapping(value = "/weixinusers", produces = "application/json;charset=UTF-8")
    public Object findweixinlist(@RequestParam int page, @RequestParam int limit, String name, String phone) {
        //CommonResponse commonResponse = CommonResponse.success();
        try {
            return sysUserService.findweixinlist(page, limit, name, phone);
        } catch (Exception e) {
            log.error("find admin users error:{}", e);

        }
        return null;
    }

    /**
     * 获取可回收价格列表
     *
     * @param page  页码
     * @param limit 每页数据量
     * @return 管理帐号列表
     */
    @ApiOperation(value = "获取可回收价格列表", notes = "获取可回收价格列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数据量", required = true, dataType = "Long")
    })
    @GetMapping(value = "/price", produces = "application/json;charset=UTF-8")
    public Object getprice(@RequestParam int page, @RequestParam int limit, String xqid, String date) {
        //CommonResponse commonResponse = CommonResponse.success();
        try {
            return sysUserService.getprice(page, limit, xqid, date);
        } catch (Exception e) {
            log.error("find admin users error:{}", e);

        }
        return null;
    }

    /**
     * 获取垃圾投递记录
     *
     * @param page  页码
     * @param limit 每页数据量
     * @return 管理帐号列表
     */
    @ApiOperation(value = "获取垃圾投递记录", notes = "获取垃圾投递记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数据量", required = true, dataType = "Long")
    })
    @GetMapping(value = "/refuselist", produces = "application/json;charset=UTF-8")
    public Object getRefuseList(@RequestParam int page, @RequestParam int limit, String type, String phone, String name) {
        //CommonResponse commonResponse = CommonResponse.success();
        try {
            return sysUserService.getRefuseList(page, limit, type, phone, name);
        } catch (Exception e) {
            log.error("find admin users error:{}", e);

        }
        return null;
    }

    /**
     * 判断垃圾是否正确
     */
    @ApiOperation(value = "判断垃圾是否正确", notes = "判断垃圾是否正确")
    @PostMapping(value = "/pdrefuse", produces = "application/json;charset=UTF-8")
    public Object pdrefuse(HttpServletRequest request) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            sysUserService.pdrefuse(request);
        } catch (Exception e) {
            log.error("delete admin user error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 获取大件预约记录后台管理
     *
     * @param page  页码
     * @param limit 每页数据量
     * @return 获取大件预约记录后台管理
     */
    @ApiOperation(value = "获取大件预约记录后台管理", notes = "获取大件预约记录后台管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数据量", required = true, dataType = "Long")
    })
    @GetMapping(value = "/appointmentlist", produces = "application/json;charset=UTF-8")
    public Object getAppointmentList(@RequestParam int page, @RequestParam int limit, String name, String phone) {
        //CommonResponse commonResponse = CommonResponse.success();
        try {
            return sysUserService.getAppointmentList(page, limit, name, phone);
        } catch (Exception e) {
            log.error("find admin users error:{}", e);

        }
        return null;
    }

    /**
     * 获取回收员列表
     *
     * @param page  页码
     * @param limit 每页数据量
     * @return 获取回收员列表
     */
    @ApiOperation(value = "获取回收员列表", notes = "获取回收员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数据量", required = true, dataType = "Long")
    })
    @GetMapping(value = "/appuserlist", produces = "application/json;charset=UTF-8")
    public Object appUserList(@RequestParam int page, @RequestParam int limit, String phone, String name) {
        //CommonResponse commonResponse = CommonResponse.success();
        try {
            return sysUserService.appUserList(page, limit, phone, name);
        } catch (Exception e) {
            log.error("find admin users error:{}", e);

        }
        return null;
    }

    /**
     * 获取投递垃圾统计
     *
     * @return 获取回收员列表
     */
    @ApiOperation(value = "获取投递垃圾统计", notes = "获取投递垃圾统计")

    @GetMapping(value = "/getRefuseTj", produces = "application/json;charset=UTF-8")
    public Object getRefuseTj() {
        //CommonResponse commonResponse = CommonResponse.success();
        try {
            return sysUserService.getRefuseTj();
        } catch (Exception e) {
            log.error("find admin users error:{}", e);

        }
        return null;
    }

    /**
     * 指派回收员
     */
    @ApiOperation(value = "指派回收员", notes = "指派回收员")
    @PostMapping(value = "/zphsy", produces = "application/json;charset=UTF-8")
    public CommonResponse zphsy(HttpServletRequest request) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            sysUserService.zphsy(request);
        } catch (Exception e) {
            log.error("delete admin user error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 修改用户积分
     */
    @ApiOperation(value = "修改用户积分", notes = "修改用户积分")
    @PostMapping(value = "/updatjf", produces = "application/json;charset=UTF-8")
    public CommonResponse updatjf(HttpServletRequest request) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            sysUserService.updatjf(request);
        } catch (Exception e) {
            log.error("delete admin user error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 审核回收员
     */
    @ApiOperation(value = "审核回收员", notes = "审核回收员")
    @PostMapping(value = "/updateappuser", produces = "application/json;charset=UTF-8")
    public CommonResponse updateappuser(HttpServletRequest request) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            sysUserService.updateappuser(request);
        } catch (Exception e) {
            log.error("delete admin user error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }
}
